//
//  UIImageView+SDWebImage.m
//  TYZ_SC
//
//  Created by 李贤立 on 2017/12/18.
//  Copyright © 2017年 Capfer. All rights reserved.
//

#import "UIImageView+SDWebImage.h"
#import "UIImageView+WebCache.h"
#import <Photos/Photos.h>
@implementation UIImageView (SDWebImage)
- (void)loadURLImageWithURL:(NSURL *)URL {
    [self sd_setImageWithURL:URL placeholderImage:[UIImage playceholderImageWithPlayceholderImageType:PlayceholderImageTypeDefault]];
}
- (void)loadURLImageWithURLString:(NSString *)urlString {
    [self loadURLImageWithURLString:urlString playceholderImageType:PlayceholderImageTypeDefault];
}
- (void)loadURLImageWithURLString:(NSString *)urlString playceholderImageType:(PlayceholderImageType)playceholderImageType {
    [self sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage playceholderImageWithPlayceholderImageType:playceholderImageType]];
}
- (void)loadAssertImageWithURL:(NSURL *)imageURL {
    PHFetchResult*result = [PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil];
    PHAsset *asset = [result firstObject];
    PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
    [[PHImageManager defaultManager] requestImageDataForAsset:asset options:phImageRequestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
        self.image = [UIImage imageWithData:imageData];
    }];
}

+ (void)loadAssertImageWithURL:(NSURL *)imageURL complement:(void(^)(UIImage *image))complement {
    PHFetchResult*result = [PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil];
    PHAsset *asset = [result firstObject];
    PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
    [[PHImageManager defaultManager] requestImageDataForAsset:asset options:phImageRequestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
        if (complement) {
            complement([UIImage imageWithData:imageData]);
        }
    }];
}
@end
